package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.telecom.ConnectionService;
import android.view.View;

import kotlin.reflect.KVariance;

public class ServiceMainActivity extends AppCompatActivity implements View.OnClickListener {

    public ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            System.out.println("componentName" + componentName + "iBinder" + iBinder);
            System.out.println("服务连接成功！");
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            System.out.println("服务断开成功！");
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service_main);
        findViewById(R.id.begin_btn).setOnClickListener(this);
        findViewById(R.id.stop_btn).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent();
        intent.setAction("com.example.myapplication.service.TEST_SERVICE");
        intent.setPackage(getPackageName());
        switch (view.getId()){
            case R.id.begin_btn:
                bindService(intent,serviceConnection, Context.BIND_AUTO_CREATE);
                //startService(intent);
                break;
            case R.id.stop_btn:
                //stopService(intent);
                if(serviceConnection != null){
                    unbindService(serviceConnection);
                }
                break;
        }
    }
}